package com.winning.ds.role.controller;

import com.winning.ds.common.controller.BaseController;
import com.winning.ds.common.controller.CommonApi;
import com.winning.ds.common.entity.MenuTreeEntity;
import com.winning.ds.common.entity.PageBean;
import com.winning.ds.common.entity.SysCzjsQxEntity;
import com.winning.ds.common.entity.TreeEntity;
import com.winning.ds.role.entity.SysCzjsEntity;
import com.winning.ds.role.entity.SysCzjsGnsjqxEntity;
import com.winning.ds.role.entity.SysZcjgQxEntity;
import com.winning.ds.role.service.SysCzjsService;
import com.winning.ds.common.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/** @Author: nxs@winning.com.cn @Description: 角色模块 @Date: 2018-07-05 10:50:08 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

  @Autowired private SysCzjsService sysCzjsService;

  @Autowired private CommonApi commonApi;

  /**
   * 保存
   *
   * @param sysczjs
   * @return ResponseEntity
   */
  @RequestMapping(value = "/save", method = RequestMethod.POST)
  public ResponseEntity save(HttpServletRequest request, @RequestBody SysCzjsEntity sysczjs) {

    if (!Common.validateParams(sysczjs.getJsmc())) {
      return ResponseEntity.error(201, "角色名称不能为空");
    }
    // String jgbm = getJgbm();
    String uuid = UUIDUtils.getUUID();
    sysczjs.setJsbm(uuid);
    sysczjs.setJsdm(uuid);
    //  sysczjs.setJgbm(jgbm);
    sysCzjsService.save(sysczjs);
    return ResponseEntity.ok();
  }

  /**
   * 更新
   *
   * @param sysczjs
   * @return ResponseEntity
   */
  @RequestMapping(value = "/update", method = RequestMethod.POST)
  public ResponseEntity update(HttpServletRequest request, @RequestBody SysCzjsEntity sysczjs) {
    sysCzjsService.update(sysczjs);
    return ResponseEntity.ok();
  }

  /**
   * 查询角色列表
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/rolelist", method = RequestMethod.POST)
  public ResponseEntity findRoleList(HttpServletRequest request, @RequestBody Map paraMaps) {
    PageBean page =
        (PageBean)
            ConverterUtils.map2ObjectWithoutNullValue(
                (Map<String, String>) paraMaps.get("page"), PageBean.class);
    page.setOrderColumn("order by t1.cjsj desc");
    paraMaps.put("page", page);
    List<SysCzjsEntity> list;
    if ("".equals(paraMaps.get("jgbm")) || null == paraMaps.get("jgbm")) {
      paraMaps.put("jgbm", getJgbm());
    }
    list = sysCzjsService.queryList(paraMaps);
    return PaginationResponseEntity.ok(page).put("rows", list);
  }

  /**
   * 查询角色列表 --根据jgbm参数
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/rolelist/{jgdm}", method = RequestMethod.GET)
  public ResponseEntity findRoleListParams(HttpServletRequest request, @PathVariable String jgdm) {
    Map map = new HashMap();
    map.put("jgbm", jgdm);
    List<SysCzjsEntity> list = sysCzjsService.queryList(map);
    return ResponseEntity.ok().put("list", list);
  }

  /**
   * 机构用户管理--查询角色列表 --根据jgbm参数
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/rolelistJgyhgl/{jgdm}", method = RequestMethod.GET)
  public ResponseEntity rolelistJgyhgl(HttpServletRequest request, @PathVariable String jgdm) {
    Map map = new HashMap();
    map.put("jgbm", jgdm);
    List<SysCzjsEntity> list = sysCzjsService.queryListJgyhgl(map);
    return ResponseEntity.ok().put("list", list);
  }

  /**
   * 删除角色
   *
   * @param jsdm
   * @return ResponseEntity
   */
  @RequestMapping(value = "/delrole/{jsdm}", method = RequestMethod.DELETE)
  public ResponseEntity delRoleList(@PathVariable String jsdm) {
    sysCzjsService.deleteRole(jsdm);
    return ResponseEntity.ok();
  }

  /**
   * 根据角色代码获取角色详情
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/getroleinfo/{jsdm}", method = RequestMethod.GET)
  public ResponseEntity findRoleListParams(@PathVariable String jsdm) {

    SysCzjsEntity sce = sysCzjsService.getRoleInfo(jsdm);
    return ResponseEntity.ok().put("sce", sce);
  }

  /**
   * 角色复制
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/copyrole", method = RequestMethod.GET)
  public ResponseEntity copyRole(@RequestParam Map<String, String> map) {

    if (!Common.validateParams(map.get("roleCheckList"))) {
      return ResponseEntity.error(201, "请选择角色");
    }
    if (!Common.validateParams(map.get("ztreeCheckList"))) {
      return ResponseEntity.error(201, "请选择机构");
    }
    // 删除当前机构用户选中的节点
    String[] ztreelist = map.get("ztreeCheckList").split("\\|");
    List<String> list = Arrays.asList(ztreelist); // 将数组转换为list集合
    List<String> arrayList = new ArrayList<String>(list);
    if (list.contains(getJgbm())) {
      arrayList.remove(getJgbm());
    }
    sysCzjsService.copyRole(map.get("roleCheckList"), arrayList);
    return ResponseEntity.ok();
  }

  /**
   * 获取机构权限菜单树--根据参数 机构编码（包含按钮）
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/getmenutree/{jgbm}", method = RequestMethod.GET)
  public ResponseEntity getMenuTree(@PathVariable String jgbm) {
    MenuTreeUtil mtu = new MenuTreeUtil();
    // List<MenuTreeEntity> list =  sysCzjsService.getMenuTree(jgbm);
    // List<Object> menuTreeList = mtu.menuTreeList(list);
    // List<TreeEntity> list =  sysCzjsService.getZcjgqx(jgbm);

    List<MenuTreeEntity> list = sysCzjsService.getZcjgqx(jgbm);
    List<Object> menuTreeList = mtu.menuTreeList(list);
    return ResponseEntity.ok().put("menuTreeList", menuTreeList);
  }

  /**
   * 根据当前登录机构编码查询机构权限
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/getzcjgqx", method = RequestMethod.GET)
  public ResponseEntity getZcjgqx() {
    // List<TreeEntity> list =  sysCzjsService.getMenu();

    List<MenuTreeEntity> list = sysCzjsService.getMenu();
    MenuTreeUtil mt = new MenuTreeUtil();
    List<Object> muTreeList = mt.menuTreeList(list);

    return ResponseEntity.ok().put("sysGncdList", muTreeList);
  }

  /**
   * 保存机构权限
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/savejgqx", method = RequestMethod.POST)
  public ResponseEntity saveJgqx(
      @RequestParam("jgqxCheckList") String jgqxCheckList, @RequestParam("jgbm") String jgbm) {

    if (!Common.validateParams(jgbm)) {
      return ResponseEntity.error(201, "机构编码不能为空");
    }
    //        if(getJgbm().equals(jgbm))
    //        {
    //            return ResponseEntity.error(201, "不能设置当前机构权限");
    //        }

    sysCzjsService.saveJgqx(jgqxCheckList, jgbm);
    return ResponseEntity.ok();
  }

  /**
   * 选中机构,获取选中机构的权限列表
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/getcheckzcjgqx/{jgbm}", method = RequestMethod.GET)
  public ResponseEntity getCheckZcjgqx(@PathVariable String jgbm) {
    List<SysZcjgQxEntity> list = sysCzjsService.querySysZcjgqx(jgbm);
    return ResponseEntity.ok().put("sysGncdList", list);
  }

  /**
   * 根据机构编码查询 机构权限 ,用于前端判断
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/getzcjgqxbyjgbm//{jgbm}", method = RequestMethod.GET)
  public ResponseEntity getZcjgqxByJgbm(@PathVariable String jgbm) {
    List<SysZcjgQxEntity> list = sysCzjsService.querySysZcjgqx(jgbm);
    return ResponseEntity.ok().put("sysGncdList", list);
  }

  /**
   * 保存 功能权限
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/savegnqx", method = RequestMethod.POST)
  public ResponseEntity saveJgGnqx(
      @RequestParam("ztreeCheckList") String ztreeCheckList,
      @RequestParam("jsdm") String jsdm,
      @RequestParam("jgbm") String jgbm) {

    if (!Common.validateParams(ztreeCheckList)) {
      return ResponseEntity.error(201, "功能权限不能为空");
    }
    if (!Common.validateParams(jgbm)) {
      return ResponseEntity.error(201, "机构编码不能为空");
    }
    if (!Common.validateParams(jsdm)) {
      return ResponseEntity.error(201, "无法获取角色编码");
    }

    sysCzjsService.saveGnqx(ztreeCheckList, jsdm, jgbm);
    return ResponseEntity.ok();
  }

  /**
   * 获取 功能权限
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/getGnqxCheck", method = RequestMethod.GET)
  public ResponseEntity getGnqxCheck(@RequestParam String jgbm, @RequestParam String jsdm) {

    if (!Common.validateParams(jgbm)) {
      return ResponseEntity.error(201, "机构编码不能为空");
    }
    if (!Common.validateParams(jsdm)) {
      return ResponseEntity.error(201, "无法获取角色编码");
    }

    List<SysCzjsQxEntity> list = sysCzjsService.getGnqxCheck(jgbm, jsdm);
    return ResponseEntity.ok().put("sysGnqxCheckList", list);
  }

  /**
   * 获取 数据权限
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/getSjqxCheck", method = RequestMethod.GET)
  public ResponseEntity getSjqxCheck(@RequestParam String jgbm, @RequestParam String jsdm) {

    if (!Common.validateParams(jgbm)) {
      return ResponseEntity.error(201, "机构编码不能为空");
    }
    if (!Common.validateParams(jsdm)) {
      return ResponseEntity.error(201, "无法获取角色编码");
    }

    List<SysCzjsGnsjqxEntity> list = sysCzjsService.getSjqxCheck(jgbm, jsdm);
    return ResponseEntity.ok().put("sysSjqxCheckList", list);
  }

  /**
   * 保存 数据权限
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/savesjqx", method = RequestMethod.POST)
  public ResponseEntity saveJgSjqx(
      @RequestParam("ztreeCheckList") String ztreeCheckList,
      @RequestParam("jsdm") String jsdm,
      @RequestParam("jgbm") String jgbm) {

    if (!Common.validateParams(ztreeCheckList)) {
      return ResponseEntity.error(201, "数据权限不能为空");
    }
    if (!Common.validateParams(jgbm)) {
      return ResponseEntity.error(201, "机构编码不能为空");
    }
    if (!Common.validateParams(jsdm)) {
      return ResponseEntity.error(201, "无法获取角色编码");
    }

    sysCzjsService.saveJgSjqx(ztreeCheckList, jsdm, jgbm);
    return ResponseEntity.ok();
  }

  /**
   * 机构权限复制
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/copyjgqx", method = RequestMethod.POST)
  public ResponseEntity copyRole(
      @RequestParam("ztreeCheckList") String ztreeCheckList, @RequestParam("jgbm") String jgbm) {

    if (!Common.validateParams(ztreeCheckList)) {
      return ResponseEntity.error(201, "请选择机构");
    }
    if (!Common.validateParams(jgbm)) {
      return ResponseEntity.error(201, "机构编码不能为空");
    }

    // 删除当前机构用户选中的节点
    String[] ztreelist = ztreeCheckList.split("\\|");
    List<String> list = Arrays.asList(ztreelist); // 将数组转换为list集合
    List<String> arrayList = new ArrayList<String>(list);
    sysCzjsService.copyJgqx(jgbm, arrayList);
    return ResponseEntity.ok();
  }

  /**
   * 获取数据权限-根据机构编码
   *
   * @return ResponseEntity
   */
  @RequestMapping(value = "/getSjqxTree/{jgbm}", method = RequestMethod.GET)
  public ResponseEntity getSjqzTree(@PathVariable String jgbm) {
    if (!Common.validateParams(jgbm)) {
      return ResponseEntity.error(201, "机构编码不能为空");
    }
    List<TreeEntity> list = sysCzjsService.getSjqzTree(jgbm);
    return ResponseEntity.ok().put("sjqxTreeList", list);
  }
}
